const Koa = require("koa");
const cors = require("@koa/cors");
const mockList = require("./mock/index");
const bodyParser = require("koa-bodyparser");
const app = new Koa();

// 跨域
app.use(cors());
// 解析请求体
app.use(bodyParser());

// 配置常量（新增）
const MOCK_DELAY = { min: 0, max: 100 }; // 延迟时间范围

// 模拟延迟（类似 mockjs 的 Mock.setup）
app.use(async (ctx, next) => {
  const delay =
    Math.floor(Math.random() * (MOCK_DELAY.max - MOCK_DELAY.min)) +
    MOCK_DELAY.min; // 200-600ms
  await new Promise((resolve) => setTimeout(resolve, delay));
  await next();
});

// 挂载 mock 路由
mockList.forEach((router) => {
  app.use(router.routes()).use(router.allowedMethods());
});

app.listen(3000, () => {
  console.log("🚀 mock-server 运行在 http://localhost:3000");
});
